<?php

namespace app\models;

use app\config\Constants;
use Yii;
use yii\helpers\Json;

/**
 * This is the model class for table "goods_order".
 *
 * @property int $order_id 自增ID
 * @property int $merchant_id 商户ID,取merchant
 * @property string $merchant_name 商户名称
 * @property string $merchant_desc 商户描述
 * @property string $merchant_thumbnail 商户店铺缩略图
 * @property string $merchant_phone 商户电话
 * @property int $user_id 用户ID
 * @property string $order_no 订单号
 * @property string $goods_item 商品集合
 * @property float $total_price 订单金额
 * @property float $delivery_price 配送金额
 * @property float $coupon_price 优惠金额
 * @property float $real_price 实付金额
 * @property string $activity_info 活动信息
 * @property string $delivery_time 送达时间
 * @property string $pay_method 支付方式:1货到付款,2微信,3支付宝
 * @property string $name 姓名
 * @property string $mobile 联系电话
 * @property string $address 详细地址
 * @property string $remark 备注
 * @property int $status 订单状态:1待接单,2取消订单,3商户已接单,4完成订单,5待调单
 * @property int $pay_status 支付状态:1待付款,2取消支付,3已付款,4已退款
 * @property int $is_adjust 是否调单:1否,2是
 * @property string $add_time 添加时间
 * @property string $end_time 完成时间
 *
 * @property Merchant $merchant
 * @property User $user
 * @property GoodsOrderCredential[] $goodsOrderCredentials
 * @property PaymentLog $paymentLog
 */
class GoodsOrder extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'goods_order';
    }

    /**
     * @return mixed
     */
    public function getGoodsItem(){
        return Json::decode($this->goods_item);
    }

    /**
     * Gets query for [[Merchant]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMerchant()
    {
        return $this->hasOne(Merchant::className(), ['merchant_id' => 'merchant_id']);
    }

    /**
     * Gets query for [[User]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['user_id' => 'user_id']);
    }

    /**
     * Gets query for [[GoodsOrderCredentials]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getGoodsOrderCredentials()
    {
        return $this->hasMany(GoodsOrderCredential::className(), ['order_id' => 'order_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPaymentLog(){
        return $this->hasOne(PaymentLog::className(),['order_id'=>'order_id'])->where("pay_status != " . Constants::PAY_STATUS_WAIT);
    }
}
